﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Globalization;
using System.Collections.ObjectModel;
using FXSStation.Modules.SharedInfrastructureModule.BusinessClasses.Chart;



namespace FXSStation.Modules.ChartModule.ChartData
{
    class ImportService
    {
        #region Fields
                /// <summary>
                /// 
                /// </summary>
        public ObservableCollection<StockItem> _row_chart_data = new ObservableCollection<StockItem>();
                /// <summary>
                /// 
                /// </summary>
        private StockItem _data_map;
                /// <summary>
                /// 
                /// </summary>
                private string _line = string.Empty;
        #endregion
 
        /// <summary>
        /// Load data from "cvs" file
        /// </summary>
        /// <param name="path">path</param>
        /// <returns></returns>
        public bool LoadData(string path)
        {
            
            StreamReader reader = null;   
            try
            {

              reader   = new StreamReader(path);

                char[] delimeter = { ',' };

                string line = string.Empty;
                while ((line = reader.ReadLine()) != null)
                {
                    //Construct a DataMap object
                    _data_map = new StockItem();

                    string[] dataLine = line.Split(delimeter);

                    DateTime time = DateTime.Parse(dataLine[0]);
                    
                    //Split hour and minutes
                    char[] delimeteHour = { ':' };
                    string [] hourMin = dataLine[1].Split(delimeteHour);
                    DateTime htime = time.AddHours(int.Parse(hourMin[0]));
                    _data_map.Date =  htime.AddMinutes(int.Parse(hourMin[1]));
                    
                    //Debugger.Break();

                    _data_map.Open = double.Parse(dataLine[2],CultureInfo.InvariantCulture);

                    _data_map.High = double.Parse(dataLine[3], CultureInfo.InvariantCulture);

                    _data_map.Low = double.Parse(dataLine[4], CultureInfo.InvariantCulture);

                    _data_map.Close = double.Parse(dataLine[5], CultureInfo.InvariantCulture);

                   // _data_map.Volume = int.Parse(dataLine[6], CultureInfo.InvariantCulture);
                   
                    //Add constructed object to the collection
                    _row_chart_data.Add(_data_map);
                    if (_row_chart_data.Count > 200)
                        break;
                }
                    
                return true;
            }
            catch (IOException ex){
                Debug.WriteLine(ex.StackTrace);
            }
           
            finally {

                reader.Close();
            }     
#region old code 


            //Microsoft.Office.Interop.Excel.ApplicationClass app = new ApplicationClass();
            //app.Workbooks.Open(file, 0, true, 5, "", "", true,
            //        Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                                                                                                                                                                                                                                                                                                                         

            //if (excel != null)
            //{
            //    //DateTime date=  DateTime.FromOADate("double value ");
            //    foreach (WorksheetRow row in excel.Worksheets[0].Rows)
            //    {
            //        _data_map = new DataMap();
                 
            //        foreach (WorksheetCell cell in row.Cells)
            //        {
            //            switch (cell.ColumnIndex)
            //            {
            //                case 1:
            //                    double date_temp =  double.Parse(cell.Value.ToString());
            //                    _data_map.Date = DateTime.FromOADate(date_temp);
                                
            //                    #region old code
                                
            //                    //temp = cell.Value.ToString();

            //                    //if (temp.Length == 5)
            //                    //{
            //                    //    hour = temp.Substring(0, 2);
            //                    //    minutes = temp.Substring(3, 2);
            //                    //}
            //                    //else
            //                    //{
            //                    //    hour = temp.Substring(0, 1);
            //                    //    minutes = temp.Substring(2, 2);
            //                    //}

            //                    //temp = null;
            //                    //_data_map.Date = new DateTime(int.Parse(year), int.Parse(month),
            //                    //    int.Parse(day), int.Parse(hour), int.Parse(minutes), seconds);
            //                    #endregion  
            //                    break;
            //                case 2:
            //                    _data_map.OpenPrice = double.Parse(cell.Value.ToString());
            //                    break;
            //                case 3:
            //                    _data_map.HightPrice = double.Parse(cell.Value.ToString());
            //                    break;
            //                case 4:
            //                    _data_map.LowPrice = double.Parse(cell.Value.ToString());
            //                    break;
            //                case 5:
            //                    _data_map.ClosePrice = double.Parse(cell.Value.ToString());
            //                    break;
            //                case 6: 
            //                    _data_map.Volume = Int32.Parse(cell.Value.ToString());
            //                    break;
           //
                       // }//end switch   
                   // }//end nested foreach

                   // _row_chart_table.Add(_data_map);
                //}
 
              //  return true;
            //
            //}
           // else
            //{
              //  return false;
          //  }
    #endregion 
            return false;
        }
      
    }
}
